//
//  SLCAMediaTimingController.m
//  Objective-C
//
//  Created by S_Line on 2018/7/3.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLCAMediaTimingController.h"

@interface SLCAMediaTimingController ()<CAAnimationDelegate>

@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (weak, nonatomic) IBOutlet UITextField *durationTextField;
@property (weak, nonatomic) IBOutlet UITextField *repeatCountTextField;
@property (weak, nonatomic) IBOutlet UIButton *startButton;

@property (weak, nonatomic) CALayer *shipLayer;

@property (weak, nonatomic) IBOutlet UISlider *timeOffsetSlider;
@property (weak, nonatomic) IBOutlet UILabel *timeOffsetLabel;
@property (weak, nonatomic) IBOutlet UISlider *speedSlider;
@property (weak, nonatomic) IBOutlet UILabel *speedLabel;
@property (nonatomic, strong) UIBezierPath *bezierPath;
@property (weak, nonatomic) CALayer *shipLayer1;

@end

@implementation SLCAMediaTimingController

- (void)initNavBar {
    self.navTitle = @"MediaTiming协议";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initView];
}

- (void)initView {
    CGFloat containerViewW = self.containerView.bounds.size.width;
    CGFloat containerViewH = self.containerView.bounds.size.height;
    
    // add the ship
    CALayer *shipLayer = [CALayer layer];
    self.shipLayer = shipLayer;
    self.shipLayer.frame = CGRectMake(0, 0, 80, 60);
    self.shipLayer.position = CGPointMake(150, 120);
    self.shipLayer.contents = (__bridge id)[UIImage imageNamed:@"caShip"].CGImage;
    [self.containerView.layer addSublayer:self.shipLayer];
    
    // add the door
    CALayer *doorLayer = [CALayer layer];
    doorLayer.frame = CGRectMake(0, 0, 120, 144);
    doorLayer.position = CGPointMake(self.containerView.frame.size.width / 2.0 - 60, self.containerView.frame.size.height / 2.0 - 50);
    doorLayer.anchorPoint = CGPointMake(0.0, 0.5);
    doorLayer.contents = (__bridge id)[UIImage imageNamed:@"caDoor"].CGImage;
    [self.containerView.layer addSublayer:doorLayer];
    
    // apply perspective transform
    CATransform3D perspective = CATransform3DIdentity;
    perspective.m34 = -1.0 / 500.0;
    self.containerView.layer.sublayerTransform = perspective;
    // apply swinging animation
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.rotation.y";
    animation.toValue = @(M_PI_2);
    animation.duration = 2.0;
    animation.repeatDuration = INFINITY;
    animation.autoreverses = true;
    [doorLayer addAnimation:animation forKey:nil];
    
    // create a path
    CGFloat offset = 100.0;
    CGPoint startPoint = CGPointMake(10, containerViewH - 150 - offset);
    CGPoint endPoint = CGPointMake(containerViewW - 10, containerViewH - 150 - offset);
    CGFloat bezierWidth = containerViewW - 20;
    self.bezierPath = [[UIBezierPath alloc] init];
    [self.bezierPath moveToPoint:startPoint];
    [self.bezierPath addCurveToPoint:endPoint controlPoint1:CGPointMake(bezierWidth / 4.0 + 10, containerViewH - 300.0 - offset) controlPoint2:CGPointMake(bezierWidth / 4.0 * 3.0 + 10, containerViewH - 0.0 - offset)];
    
    // draw the path using a CAShapeLayer
    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.path = self.bezierPath.CGPath;
    pathLayer.fillColor = [UIColor clearColor].CGColor;
    pathLayer.strokeColor = [UIColor blackColor].CGColor;
    pathLayer.lineWidth = 1.0;
    [self.containerView.layer addSublayer:pathLayer];
    
    // add the ship1
    CALayer *shipLayer1 = [CALayer layer];
    self.shipLayer1 = shipLayer1;
    self.shipLayer1.frame = CGRectMake(0, 0, 40, 30);
    self.shipLayer1.position = startPoint;
    self.shipLayer1.contents = (__bridge id)[UIImage imageNamed:@"caShip"].CGImage;
    [self.containerView.layer addSublayer:self.shipLayer1];
    
    [self updateSliders];
}

- (void)setControlsEnable:(BOOL)enable {
    for (UIControl *control in @[self.durationTextField, self.repeatCountTextField, self.startButton]) {
        control.enabled = enable;
        control.alpha = enable ? 1.0 : 0.25;
    }
}

- (IBAction)startButtonClick:(UIButton *)sender {
    CFTimeInterval duration = [self.durationTextField.text doubleValue];
    float repeatCount = [self.repeatCountTextField.text floatValue];
    // animate the ship rotation
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.rotation";
    animation.duration = duration;
    animation.repeatCount = repeatCount;
    animation.byValue = @(M_PI * 2);
    animation.delegate = self;
    [self.shipLayer addAnimation:animation forKey:@"rotateAnimation"];
    // disable controls
    [self setControlsEnable:false];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    // reenable controls
    [self setControlsEnable:true];
}

- (void)updateSliders {
    CFTimeInterval timeOffset = self.timeOffsetSlider.value;
    self.timeOffsetLabel.text = [NSString stringWithFormat:@"%.2f", timeOffset];
    float speed = self.speedSlider.value;
    self.speedLabel.text = [NSString stringWithFormat:@"%.2f", speed];
}

- (IBAction)playButtonClick:(UIButton *)sender {
    [self updateSliders];
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position";
    animation.timeOffset = self.timeOffsetSlider.value;
    animation.speed = self.speedSlider.value;
    animation.duration = 1.0;
    animation.path = self.bezierPath.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    [self.shipLayer1 addAnimation:animation forKey:@"slide"];
}

@end
